package binarySearch;

public class PeakIndexInAMountainArray852 {
    public int peakIndexInMountainArray(int[] arr) {
        int begin = 0;
        int end = arr.length - 1;
        while (begin <= end) {
            int mid = (begin + end) / 2;
            if(mid==0){
                begin=mid+1;
                continue;
            }
            if(mid==arr.length-1){
                end = mid-1;
                continue;
            }
            if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) {
                return mid;
            }
            if (arr[mid] < arr[mid + 1]) {
                begin = mid + 1;
            }
            if (arr[mid] < arr[mid - 1]) {
                end = mid - 1;
            }
        }
        return -1;
    }
}
